﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{
    public class subCatogoryController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static subCatogoryInfo getbyID(int subCatogoryID)
        {
            subCatogoryInfo objsubcato = new subCatogoryInfo();
            using (SqlCommand cmd = new SqlCommand("getIDSubCatogory", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@subCatogoryID", subCatogoryID));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objsubcato.subCatogoryID = dr.GetInt32(0);
                        objsubcato.subCatogoryName = dr.GetString(1);
                        objsubcato.catogoryID = dr.GetInt32(2);
                    }
                }
                dr.Close();
            }
            return objsubcato;
        }

        #endregion

        #region getbyAll
        public static DataTable ListsubCatogory()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listSubCatogory", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertSubCatogory(subCatogoryInfo objSub)
        {
            using (SqlCommand cmd = new SqlCommand("insertSubCatogory", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@subCatogoryID", objSub.subCatogoryID));
                cmd.Parameters.Add(new SqlParameter("@subCatogoryName", objSub.subCatogoryName));
                cmd.Parameters.Add(new SqlParameter("@catogoryID", objSub.catogoryID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updatesubCatogory(subCatogoryInfo objSub)
        {
            using (SqlCommand cmd = new SqlCommand("updateSubCatogory", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@subCatogoryID", objSub.subCatogoryID));
                cmd.Parameters.Add(new SqlParameter("@subCatogoryName", objSub.subCatogoryName));
                cmd.Parameters.Add(new SqlParameter("@catogoryID", objSub.catogoryID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deletesubCatogory(int subCatogoryID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteSubCatogory", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@subCatogoryID", subCatogoryID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region MyRegion
        
        #endregion
        //#region checkExist
        //public static bool checkExist(string subcatogoryID)
        //{
        //    bool test = false;
        //    using (SqlCommand cmd = new SqlCommand("checkExist_subCato", con))
        //    {
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Parameters.Add(new SqlParameter("@subCatogoryID", subcatogoryID));
        //        SqlDataReader dr = cmd.ExecuteReader();
        //        if (dr.HasRows)
        //        {
        //            test = true;
        //        }
        //        dr.Close();
        //    }
        //    return test;
        //}
        //#endregion
        #region checksubCatogoryID
        public static bool checksubCatogoryID(int subCatogoryID)
        {
            SqlCommand cmd = new SqlCommand("checksubCatogoryID", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@subCatogoryID", subCatogoryID));
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                return true;
            }
            dr.Close();
            return false;
        }
        #endregion
    }
}
